// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// * Copyright 2023 The Geek-AI Authors. All rights reserved.
// * Use of this source code is governed by a Apache-2.0 license
// * that can be found in the LICENSE file.
// * @Author yangjian102621@163.com
// * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

import axios from 'axios'
import {getAdminToken, getSessionId, getUserToken, removeAdminToken, removeUserToken} from "@/store/session";

axios.defaults.timeout = 180000
axios.defaults.baseURL = process.env.VUE_APP_API_HOST
// axios.defaults.withCredentials = true;
axios.defaults.headers.post['Content-Type'] = 'application/json'

// HTTP拦截器
axios.interceptors.request.use(
    config => {
        // set token
        config.headers['Chat-Token'] = getSessionId();
        config.headers['Authorization'] = getUserToken();
        config.headers['Admin-Authorization'] = getAdminToken();
        return config
    }, error => {
        return Promise.reject(error)
    })
axios.interceptors.response.use(
    response => {
        let data = response.data;
        // 新增代码 这里处理的是 导出卡密表格，返回的数据不一样
        if (response.request.responseURL.indexOf("/api/admin/redeem/export") !== -1) {
            return response
        }
        // 新增代码，因为获取验证码的请求，返回的数据 不是这个系统配置的，所以单独处理一下这个请求
        if (data.hasOwnProperty("image_url")) {
            return response
        }
        ////////////////////////////////
        if (data.code === 0) {
            return response
        } else if (data.code === 400) {
            if (response.request.responseURL.indexOf("/api/admin") !== -1) {
                removeAdminToken()
            } else {
                removeUserToken()
            }
        }
            return Promise.reject(response.data)
    }, error => {
        return Promise.reject(error)
    })


// send a http get request
export function httpGet(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            params: params
        }).then(response => {
            resolve(response.data)
        }).catch(err => {
            reject(err)
        })
    })
}

// send a http get request
export function httpGetCaptcha(url, params = {}) {
    return new Promise((resolve, reject) => {
        axios.get(url, {
            headers: {
              'x-requested-with': 'XMLHttpRequest'
            },
            params: params
        }).then(response => {
            resolve(response)
        }).catch(err => {
            reject(err)
        })
    })
}


// send a http post request
export function httpPost(url, data = {}, options = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data, options).then(response => {
            resolve(response.data)
        }).catch(err => {
            reject(err)
        })
    })
}


// send a http post request
export function httpPostExportRedeem(url, data = {}) {
    return new Promise((resolve, reject) => {
        axios.post(url, data, {
            responseType: 'blob'  // 设置响应类型为blob，以便接收二进制数据
        }).then(response => {
            resolve(response)
        }).catch(err => {
            reject(err)
        })
    })
}

